/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.resources.impl.internal;

import java.io.File;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;

import nemos.resources.IResources;

import org.osgi.framework.Bundle;

/**
 * UrlResources : The URL resources implementation.
 * 
 * @author Romain DUBOIS
 */
public class UrlResources extends ResourceBundle implements IResources<URL> {

    /** The resources base name */
    private final String mBaseName;
    /** The required extension */
    private final String mExtension;
    /** The resources bundle */
    private final Bundle mBundle;

    /**
     * Constructor.
     * 
     * @param pBaseName
     *            The resources base name
     * @param pExtension
     *            The managed files extension
     * @param pBundle
     *            The bundle of the application resources
     */
    public UrlResources(final String pBaseName, final String pExtension, final Bundle pBundle) {
        super();
        mBaseName = pBaseName;
        mExtension = pExtension;
        mBundle = pBundle;
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.util.ResourceBundle#handleGetObject(java.lang.String)
     */
    @Override
    protected URL handleGetObject(final String pKey) {
        return mBundle.getResource(mBaseName + pKey + mExtension);
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.util.ResourceBundle#getKeys()
     */
    @Override
    public Enumeration<String> getKeys() {
        return Collections.enumeration(keySet());
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.util.ResourceBundle#handleKeySet()
     */
    @Override
    protected Set<String> handleKeySet() {
        final Set<String> lKeys = new HashSet<String>();
        final Enumeration<URL> lEntries = mBundle.findEntries(mBaseName, "*" + mExtension, false); //$NON-NLS-1$

        while (lEntries.hasMoreElements()) {
            lKeys.add(new File(lEntries.nextElement().getFile()).getName());
        }

        return lKeys;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.resources.IResources#getResource(java.lang.String)
     */
    @Override
    public URL getResource(final String pKey) {
        try {
            return (URL) getObject(pKey);
        } catch (final MissingResourceException e) {
            return null;
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.resources.IResources#adapt(java.lang.Class)
     */
    @SuppressWarnings("unchecked")
    @Override
    public <U> IResources<U> adapt(final Class<U> pNewType) {
        if (pNewType.isAssignableFrom(URL.class)) {
            return (IResources<U>) this;
        }
        throw new ClassCastException(pNewType + " != " + URL.class); //$NON-NLS-1$
    }
}
